<!--
PAGE DOCUMENTATION:
==================
This page is were the received messages are viewed.
User can handle those messages from here.

Parameters that can be sent to this page:
----------------------------------------
timeZoneSelect - if the user updated his/her time settings. 
				Value: the offset from GMT chosen by the user.
delete - if the user selected a message on this page and chose to delete it. 
			Value: "delete".
			Note: must come with an index parameter as well.
play - if the user selected a NEW message on this page and chose to play it. 
			Value: "play".
			Note: 	(1) must come with index and not_read parameters.
filename - if the user chose to delete a message.
			Value: the filename of deleted message.
			Note: will be used only with delete parameter.
not_read - if the user chose to play a new message this parameter will be present.
			Value: 1.
				
recipient - if the user got to this page after asking to respond a received message. 
			Value: friend's facebook user ID.
index - Value: the DB index of the message the user performd an action on.
-->

<html>

<fb:dashboard>
<fb:action href="invite.php">
Invite some friends over!
</fb:action>
<fb:action href="sendMessage.php">
Send a voice message to a friend!
</fb:action>
<fb:action href="index.php">
VoiceMessanger main page!
</fb:action>
<fb:help href="help.php">
Help Me!
</fb:help>
</fb:dashboard>

 <fb:js-string var="dialog_fbml">  
    <div id="dialog_content">  
        <div class="dialog_loading">Please Wait...</div>  
    </div>  
</fb:js-string>

<?php

	require_once 'appinclude.php'; 
?>
<!-- make the SERVER accessible to the javascript-->
<script>
var server = '<?php echo SERVER; ?>';
</script>
<?php
 	echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href='$server/design.css?v1.2'/>";
 	echo "<script src='$server/js_features.js?v2.99'></script>";
	require_once 'sqlLib.php';
	require_once 'logsLib.php';
	require_once 'generalLib.php';

	$currentPage ='getMessages';	
	$user_id = $facebook->user;
	$sqlDb = new mysqli("localhost", "root", "", "messages");
	$sqlDb->query('SET NAMES utf8');
	
	/* check connection */
	if (mysqli_connect_errno()) {
    	documentError($user_id,$currentPage,"Connect to MYSQL failed: ".mysqli_connect_error());
    	printErrorMsg("connecting to the database");
	} else {
		logMysqliConnectionOpened($user_id,$currentPage);
		
		/* if user changed his time settings - update the DB: */
		if (isset($_REQUEST['timeZoneSelect'])) {
			$newTimeOffset = $_REQUEST['timeZoneSelect'];
			$updateRes = updateDBcolumn($sqlDb,'sessions',$user_id,'time_offset',$newTimeOffset);
			if ($updateRes) {
				logFunctionCall($user_id,'updateDBcolumn','sqlDb,sessions,'.$user_id.',time_offset,'.$newTimeOffset,$updateRes,$currentPage);
			} else {
				documentError($user_id,$currentPage,"updateDBcolumn failed");
				printErrorMsg("updating your time settings");
			}
		}
		
		/*check if the user asked to play or delete a message*/
		if (isset($_REQUEST['play']) || isset($_REQUEST['delete'])) {
			$filename = $_REQUEST['filename'];
			$index = $_REQUEST['index'];
			if (isset($_REQUEST['play'])) {
				/* play message */
//				echo "<table cellpadding=\"10\" align=\"center\"><tr><td>";
//				echo "Loading file. This might take a few seconds or more (Depending on message length).<tr> ";
//				activatePlayApplet($server,$filename);
//				logFunctionCall($user_id, "activatePlayApplet",$server.','.$filename,NULL,$currentPage);
//				echo "</td></tr></table>";
				if (isset($_REQUEST['not_read'])) {
					/*change in the DB to "already played"*/
					if(!($change = updateDBcolumn($sqlDb,"messages_data",$index,"is_read",1))){
						documentError($user_id,$currentPage,"updateDBis_read failed - index: $index");
						printErrorMsg("updating our data base");
					} else {
						logFunctionCall($user_id, 'updateDBis_read', "sqlDb,".$index,$change,$currentPage);	
					}
				}
			} else {
				/* delete message */
				/* delete row from DB */
				if (!($deleteMsgRes = deleteMessageData($sqlDb,$index))) {
					documentError($user_id,$currentPage,"deleteMessageData failed - index: $index");
					printErrorMsg("deleting the message (it may not have been deleted)");
				} else {
					logFunctionCall($user_id,'deleteMessageData',"sqlDb,".$index,$deleteMsgRes,$currentPage);
				}
				/* delete file if needed */
				$deleteFileRes = checkFileDeleteNeeded($sqlDb,$filename);
				if ($deleteFileRes==1 || $deleteFileRes==0){
					if ($deleteFileRes==1) {
						$fullFileName = $filesLocation .$filename;
						$unlinkRes = unlink($fullFileName);
						logFunctionCall($user_id,'unlink',$fullFileName,$unlinkRes,$currentPage);
					}
					logFunctionCall($user_id, 'checkFileDeleteNeeded', "sqlDb,".$filename,$deleteFileRes,$currentPage);
				} else {
					documentError($user_id,$currentPage,"checkFileDeleteNeeded failed - filename: $filename");
					printErrorMsg("deleting the message (it may not have been deleted)");
				}
			}
		}
		
		/* show page content */
		echo "<table border=\"0\" align=\"center\" cellpadding=\"10\">
				<tr>
					<td>
						<h1 class=\"tableheader\" >Messages received from your friends:</h1>
					</td>
				</tr>
				<tr>
					<td valign=\"top\" align=\"left\">";
		/* received messages table */
		if (($offsetHours = getColumnData($sqlDb,"sessions",$user_id,"time_offset"))==-13) {
			documentError($user_id,$currentPage,"getColumnData failed");
			printErrorMsg("presenting the sending time for each message (the time will be GMT)");
			$offsetHours = 0;
		} else {
			logFunctionCall($user_id,'getColumnData',"sqlDb,sessions".$user_id.",time_offset",$offsetHours,$currentPage);
		}
		if(!($tableRes = presentReceivedMessages($sqlDb,$user_id,$offsetHours))){
			documentError($user_id,$currentPage,"presentReceivedMessages failed - user_id: $user_id");
			printErrorMsg("searchig the data base for your received messages");
		} else {
			logFunctionCall($user_id,'presentReceivedMessages',"sqlDb,".$user_id.",".$offsetHours,$tableRes,$currentPage);
		}
		echo " 		</td>
				</tr>
				<tr>
					<td>";
		/* time settings */
		if (!($timeSettingRes = showTimeSettings($sqlDb,$user_id,$currentPage))) {
			documentError($user_id,$currentPage,"showTimeSettings failed");
		} else {
			logFunctionCall($user_id, 'showTimeSettings', "sqlDb,".$user_id.",".$currentPage,$timeSettingRes,$currentPage);
		}
		echo "		</td>
				</tr>
			</table>
			<img src=\"$server/womanwithcanphone_long.JPG\" align =\"right\">";

		/* update user profile */
		/* get number of new messages for user: */
		if (($numberOfNewMessages = countMessages($sqlDb,$user_id,"unread"))==-1) {
			documentError($user_id,$currentPage,"countMessages failed - user_id: $user_id, unread");
			printErrorMsg("updating your profile box. It may not be updated");
		} else {
			logFunctionCall($user_id,'countMessages','sqlDb,'.$user_id.',unread',$numberOfNewMessages,$currentPage);
		}
		/* get number of messages sent by user: */
		if (($numberOfSentMessages = countMessages($sqlDb,$user_id,"sent"))==-1) {
			documentError($user_id,$currentPage,"countMessages failed - user_id: $user_id, sent");	
			printErrorMsg("updating your profile box. It may not be updated");
		} else {
			logFunctionCall($user_id, 'countMessages', 'sqlDb,'.$user_id.',sent', $numberOfSentMessages,$currentPage);
		}
		if (!(profileBox($user_id, $numberOfNewMessages, $numberOfSentMessages, $facebook))){
			documentError($user_id,$currentPage,"profileBox failed - user_id: $user_id");
			printErrorMsg("updating your profile box. It may not be updated");
		}
		
    	/* close connection */
		$sqlDb->close();
		logMysqliConnectionClosed($user_id,$currentPage);
		}

?>
</br>
<font size="3">
This application participates in Unicell's contest. <a href="http://www.liveface.co.il/%D7%AA%D7%97%D7%A8%D7%95%D7%AA-%D7%90%D7%A4%D7%9C%D7%99%D7%A7%D7%A6%D7%99%D7%95%D7%AA-%D7%A4%D7%99%D7%99%D7%A1%D7%91%D7%95%D7%A7/apps/" > For rating and other applications. </a>
</font>
</html>
